﻿namespace CSharpPro._1013;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
public  class Demo2
{
    private List<User> users = new List<User>();
    public User CurrentUser { get; private set; }
    public string Register(string username, string password, string confirmPassword)
    {
        if (string.IsNullOrWhiteSpace(username))
        {
            return "用户名不可为空";
        }
        if (string.IsNullOrWhiteSpace(password))
        {
            return "密码不可为空";
        }
        if (string.IsNullOrWhiteSpace(confirmPassword))
        {
            return "确认密码不可为空";
        }
        if (username.Length < 3 || username.Length > 15)
        {
            return "用户名长度必须为 3 - 15 个字符";
        }
        if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
        {
            return "用户名只能包含字母、数字、下划线";
        }
        if (password.Length < 6 || password.Length > 20)
        {
            return "密码长度必须为 6 - 20 个字符";
        }
        if (!Regex.IsMatch(password, @"[A-Z]") || !Regex.IsMatch(password, @"[a-z]") ||
            !Regex.IsMatch(password, @"[0-9]"))
        {
            return "密码必须包含至少 1 个大写字母、1 个小写字母和 1 个数字";
        }
        if (password != confirmPassword)
        {
            return "密码与确认密码不一致";
        }
        if (users.Any(u => u.Username == username))
        {
            return "用户名已存在";
        }
        users.Add(new User { Username = username, Password = password });
        return "注册成功";
    } 
    public string Login(string username, string password)
    {
        if (string.IsNullOrWhiteSpace(username))
        {
            return "用户名不可为空";
        }
        if (string.IsNullOrWhiteSpace(password))
        {
            return "密码不可为空";
        }
        User user = users.FirstOrDefault(u => u.Username == username);
        if (user == null)
        {
            return "用户名不存在";
        }
        if (user.Password != password)
        {
            return "密码错误";
        }
        CurrentUser = user;
        return "登录成功";
    }
}
public class User
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }
